home *** CD-ROM | disk | FTP | other *** search
- /* @(#) $Header: netupdc.c,v 1.5 91/10/25 14:21:26 deyke Exp $ */
-
- /* Net Update Client */
-
- #define _HPUX_SOURCE 1
-
- #include <sys/types.h>
-
- #include <fcntl.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/utsname.h>
- #include <unistd.h>
-
- #if defined(__TURBOC__) || defined(__STDC__)
- #define __ARGS(x) x
- #else
- #define __ARGS(x) ()
- #define const
- #endif
-
- #include "buildsaddr.h"
-
- static void pexit __ARGS((const char *s));
- static void doread __ARGS((int fd, char *buf, size_t cnt));
- static void dowrite __ARGS((int fd, const char *buf, size_t cnt));
- int main __ARGS((int argc, char **argv));
-
- /*---------------------------------------------------------------------------*/
-
- static void pexit(s)
- const char *s;
- {
- perror(s);
- exit(1);
- }
-
- /*---------------------------------------------------------------------------*/
-
- static void doread(fd, buf, cnt)
- int fd;
- char *buf;
- size_t cnt;
- {
-
- char *p = buf;
- int n;
-
- while (cnt) {
- n = read(fd, p, cnt);
- if (n < 0) pexit("read");
- if (!n) {
- printf("read(): End of file\n");
- exit(1);
- }
- p += n;
- cnt -= n;
- }
- }
-
- /*---------------------------------------------------------------------------*/
-
- static void dowrite(fd, buf, cnt)
- int fd;
- const char *buf;
- size_t cnt;
- {
-
- const char * p = buf;
- int n;
-
- while (cnt) {
- n = write(fd, p, cnt);
- if (n <= 0) pexit("write");
- p += n;
- cnt -= n;
- }
- }
-
- /*---------------------------------------------------------------------------*/
-
- int main(argc, argv)
- int argc;
- char **argv;
- {
-
- char *client;
- char *server;
- char buf[1024];
- char filename[1024];
- int addrlen;
- int fdfile;
- int fdsocket;
- int filesize;
- int i;
- int net_filesize;
- int net_i;
- struct sockaddr *addr;
- struct utsname utsbuf;
-
- alarm(6 * 3600);
-
- if (uname(&utsbuf)) pexit("uname");
-
- server = (argc < 2) ? "db0sao" : argv[1];
- client = (argc < 3) ? utsbuf.nodename : argv[2];
-
- if (getuid()) {
- printf("%s: Permission denied\n", *argv);
- exit(1);
- }
-
- umask(022);
- putenv("HOME=/users/root");
- putenv("LOGNAME=root");
- putenv("PATH=/bin:/usr/bin:/usr/local/bin:/usr/contrib/bin");
- putenv("SHELL=/bin/sh");
- putenv("TZ=MEZ-1MESZ");
-
- if (chdir("/tcp")) pexit("/tcp");
-
- if (!(addr = build_sockaddr("unix:/tcp/.sockets/netcmd", &addrlen))) {
- printf("build_sockaddr(): Failed\n");
- exit(1);
- }
-
- fdsocket = socket(addr->sa_family, SOCK_STREAM, 0);
- if (fdsocket < 0) pexit("socket");
-
- if (connect(fdsocket, addr, addrlen)) pexit("connect");
-
- strcpy(buf, "binary\n");
- dowrite(fdsocket, buf, strlen(buf));
-
- sprintf(buf, "connect tcp %s 4715\n", server);
- dowrite(fdsocket, buf, strlen(buf));
-
- dowrite(fdsocket, client, strlen(client) + 1);
-
- doread(fdsocket, (char *) &net_filesize, 4);
- filesize = ntohl(net_filesize);
-
- tmpnam(filename);
- fdfile = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
- if (fdfile < 0) pexit(filename);
- while (filesize > 0) {
- i = filesize < sizeof(buf) ? filesize : sizeof(buf);
- doread(fdsocket, buf, (unsigned) i);
- dowrite(fdfile, buf, (unsigned) i);
- filesize -= i;
- }
- if (close(fdfile)) pexit("close");
-
- sprintf(buf, "uncompress < %s | sh", filename);
- i = system(buf);
- net_i = htonl(i);
- dowrite(fdsocket, (char *) &net_i, 4);
-
- if (unlink(filename)) pexit(filename);
-
- if (!i) system("exec make");
-
- return i;
- }
-
-